
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Interfaces.ApparentModalI;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import src.ApparentModalC;
import src.General;

/**
 * Η βασική φόρμα της εφαρμογής με το μενού
 *
 * @author george
 */
public class RadioStationJFrame extends javax.swing.JFrame implements ApparentModalI {

    private JFrame next = null;
    private final JFrame caller = null;

    /**
     * Creates new form RadioStationJFrame
     *
     * @throws java.io.IOException
     */
    public RadioStationJFrame() throws IOException {
        try {
            initComponents();

            //θέτει το εικονίδιο στην μπάρα τίτλου της φόρμας
            Image applicationImage = ImageIO.read(getClass().getResource("/Images/note1.png"));
            this.setIconImage(applicationImage);

            // Get user's name
            String strUserName = null;

            if (System.getProperty("user.name") != null) {
                strUserName = System.getProperty("user.name");
            }

            // Welcome message
            if (strUserName != null) {
                this.jlblWelcomeUser.setText("Welcome user: " + strUserName);
            }
        } catch (PersistenceException prEx) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, "Υπήρξε πρόβλημα κατά την σύνδεση με την βάση δεδομένων.\r\n\r\nΕλέγξτε την σύνδεση και προσπαθήστε ξανά! ", this.getTitle() + " - Error", 0);
        } catch (Exception ex) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle() + " - Error", 0);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuItem1 = new javax.swing.JMenuItem();
        jlblWelcomeUser = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        archivesJMenu = new javax.swing.JMenu();
        artistsJMenuItem = new javax.swing.JMenuItem();
        groupsJMenuItem = new javax.swing.JMenuItem();
        groupAlbumsJMenuItem = new javax.swing.JMenuItem();
        artistAlbumsJMenuItem = new javax.swing.JMenuItem();
        playlistsJMenu = new javax.swing.JMenu();
        playListsTableJMenuItem = new javax.swing.JMenuItem();
        exitJMenu = new javax.swing.JMenu();
        closeAppJmenuItem = new javax.swing.JMenuItem();

        jMenuItem1.setText("jMenuItem1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Radio Station");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                radioStationWindowClosing(evt);
            }
        });

        jlblWelcomeUser.setText("Welcome");
        jlblWelcomeUser.setToolTipText("");

        archivesJMenu.setText("Διαχείριση αρχείων");

        artistsJMenuItem.setText("Καλλιτέχνες");
        artistsJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                artistsJMenuItemActionPerformed(evt);
            }
        });
        archivesJMenu.add(artistsJMenuItem);

        groupsJMenuItem.setText("Συγκροτήματα");
        groupsJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                groupsJMenuItemActionPerformed(evt);
            }
        });
        archivesJMenu.add(groupsJMenuItem);

        groupAlbumsJMenuItem.setText("Άλμπουμ συγκροτημάτων");
        groupAlbumsJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                groupAlbumsJMenuItemActionPerformed(evt);
            }
        });
        archivesJMenu.add(groupAlbumsJMenuItem);

        artistAlbumsJMenuItem.setText("Άλμπουμ καλλιτεχνών");
        artistAlbumsJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                artistAlbumsJMenuItemActionPerformed(evt);
            }
        });
        archivesJMenu.add(artistAlbumsJMenuItem);

        jMenuBar1.add(archivesJMenu);

        playlistsJMenu.setText("Λίστες τραγουδιών");

        playListsTableJMenuItem.setText("Πίνακας λιστών");
        playListsTableJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                playListsTableJMenuItemActionPerformed(evt);
            }
        });
        playlistsJMenu.add(playListsTableJMenuItem);

        jMenuBar1.add(playlistsJMenu);

        exitJMenu.setText("Έξοδος");

        closeAppJmenuItem.setText("Κλείσιμο εφαρμογής");
        closeAppJmenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeAppJmenuItemActionPerformed(evt);
            }
        });
        exitJMenu.add(closeAppJmenuItem);

        jMenuBar1.add(exitJMenu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jlblWelcomeUser, javax.swing.GroupLayout.PREFERRED_SIZE, 546, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(25, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlblWelcomeUser)
                .addContainerGap(249, Short.MAX_VALUE))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Εκτελείται ακόμα και όταν το παράθυρο κλείσει από το "Χ" στην μπάρα
     * τίτλου
     *
     * @param evt
     */
    private void radioStationWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_radioStationWindowClosing
        if (this.checkOnClosing())//αν είναι όλα οκ κλείνει το παράθυρο
        {
           this.dispose();
        }
    }//GEN-LAST:event_radioStationWindowClosing


    private void artistsJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_artistsJMenuItemActionPerformed
        try {
            this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            ArtistsArchiveJFrame aajf = new ArtistsArchiveJFrame(this);

            ApparentModalC.modalSetNextVisible(this, aajf);
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } catch (PersistenceException prEx) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            if (prEx.getMessage().contains("DatabaseException")) {
                JOptionPane.showMessageDialog(this, "Υπήρξε πρόβλημα κατά την σύνδεση με την βάση δεδομένων.\r\n\r\nΕλέγξτε την σύνδεση και προσπαθήστε ξανά! ", this.getTitle() + " - Error", 0);
            } else {
                JOptionPane.showMessageDialog(this, prEx.getMessage(), this.getTitle() + " - Error", 0);
            }
        } catch (Exception ex) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle() + " - Error", 0);
        }
    }//GEN-LAST:event_artistsJMenuItemActionPerformed

    private void groupsJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_groupsJMenuItemActionPerformed

        try {
            this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            GroupsTableJFrame gt = new GroupsTableJFrame(this);
            
            ApparentModalC.modalSetNextVisible(this, gt);
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } catch (PersistenceException prEx) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            if (prEx.getMessage().contains("DatabaseException")) {
                JOptionPane.showMessageDialog(this, "Υπήρξε πρόβλημα κατά την σύνδεση με την βάση δεδομένων.\r\n\r\nΕλέγξτε την σύνδεση και προσπαθήστε ξανά! ", this.getTitle() + " - Error", 0);
            } else {
                JOptionPane.showMessageDialog(this, prEx.getMessage(), this.getTitle() + " - Error", 0);
            }
        } catch (Exception ex) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle() + " - Error", 0);
        }


    }//GEN-LAST:event_groupsJMenuItemActionPerformed

    private void groupAlbumsJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_groupAlbumsJMenuItemActionPerformed
        try {
            this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            AlbumTableJFrame gatJF = new AlbumTableJFrame(this, false);
            ApparentModalC.modalSetNextVisible(this, gatJF);
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } catch (PersistenceException prEx) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            if (prEx.getMessage().contains("DatabaseException")) {
                JOptionPane.showMessageDialog(this, "Υπήρξε πρόβλημα κατά την σύνδεση με την βάση δεδομένων.\r\n\r\nΕλέγξτε την σύνδεση και προσπαθήστε ξανά! ", this.getTitle() + " - Error", 0);
            } else {
                JOptionPane.showMessageDialog(this, prEx.getMessage(), this.getTitle() + " - Error", 0);
            }
        } catch (Exception ex) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle() + " - Error", 0);
        }
    }//GEN-LAST:event_groupAlbumsJMenuItemActionPerformed

    private void artistAlbumsJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_artistAlbumsJMenuItemActionPerformed
        try {
            this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            AlbumTableJFrame aatJF = new AlbumTableJFrame(this, true);
            ApparentModalC.modalSetNextVisible(this, aatJF);
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } catch (PersistenceException prEx) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            if (prEx.getMessage().contains("DatabaseException")) {
                JOptionPane.showMessageDialog(this, "Υπήρξε πρόβλημα κατά την σύνδεση με την βάση δεδομένων.\r\n\r\nΕλέγξτε την σύνδεση και προσπαθήστε ξανά! ", this.getTitle() + " - Error", 0);
            } else {
                JOptionPane.showMessageDialog(this, prEx.getMessage(), this.getTitle() + " - Error", 0);
            }
        } catch (Exception ex) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle() + " - Error", 0);
        }
    }//GEN-LAST:event_artistAlbumsJMenuItemActionPerformed

    private void closeAppJmenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeAppJmenuItemActionPerformed
        this.radioStationWindowClosing(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
    }//GEN-LAST:event_closeAppJmenuItemActionPerformed

    private void playListsTableJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_playListsTableJMenuItemActionPerformed

        try {
            this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            EntityManager em;
            em = General.getEntityManager();

            String username;
            username = General.strUserName;

            //this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            Playlists jfPlaylists = new Playlists(this, em, username);
            //jfPlaylists.setVisible(true);
            ApparentModalC.modalSetNextVisible(this, jfPlaylists);
            //PlaylistsForm plf = new PlaylistsForm();
            //plf.setVisible(true);
            this.setEnabled(false);
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        } catch (PersistenceException prEx) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            if (prEx.getMessage().contains("DatabaseException")) {
                JOptionPane.showMessageDialog(this, "Υπήρξε πρόβλημα κατά την σύνδεση με την βάση δεδομένων.\r\n\r\nΕλέγξτε την σύνδεση και προσπαθήστε ξανά! ", this.getTitle() + " - Error", 0);
            } else {
                JOptionPane.showMessageDialog(this, prEx.getMessage(), this.getTitle() + " - Error", 0);
            }
        } catch (Exception ex) {
            this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            JOptionPane.showMessageDialog(this, ex.getMessage(), this.getTitle() + " - Error", 0);
        }

    }//GEN-LAST:event_playListsTableJMenuItemActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        //General.modalOnWindowActivated(this);
    }//GEN-LAST:event_formWindowActivated
    /**
     * Ελέγχει την κατάσταση κατά την έξοδο. Αν όλα είναι ΟΚ επιστρέφει true,
     * αλλιώς false και εμφανίζει κάποιο μήνυμα σφάλματος
     *
     * @return
     */
    private boolean checkOnClosing() {
        //έλεγχος εξόδου
        String[] options = {"NAI", "OXI"};
        int result = JOptionPane.showOptionDialog(this, "Σίγουρα θέλετε να κλείσετε την εφαρμογή;",
                this.getTitle() + " - Επιβεβαίωση εξόδου", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        return result == JOptionPane.YES_OPTION;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RadioStationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RadioStationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RadioStationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RadioStationJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new RadioStationJFrame().setVisible(true);
                    
                } catch (IOException ex) {
                    Logger.getLogger(RadioStationJFrame.class.getName()).log(Level.SEVERE, null, ex);
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "Εσωτερικό σφάλμα", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu archivesJMenu;
    private javax.swing.JMenuItem artistAlbumsJMenuItem;
    private javax.swing.JMenuItem artistsJMenuItem;
    private javax.swing.JMenuItem closeAppJmenuItem;
    private javax.swing.JMenu exitJMenu;
    private javax.swing.JMenuItem groupAlbumsJMenuItem;
    private javax.swing.JMenuItem groupsJMenuItem;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JLabel jlblWelcomeUser;
    private javax.swing.JMenuItem playListsTableJMenuItem;
    private javax.swing.JMenu playlistsJMenu;
    // End of variables declaration//GEN-END:variables

    @Override
    public Frame getCaller() {
        return this.caller;
    }

    @Override
    public Frame getNext() {
        return this.next;
    }

    @Override
    public void setCallersNextNull() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setNext(Frame w) {
        this.next = (JFrame) w;
    }
}
